﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PBOffice.Models;
using PBOffice.BLL;

public partial class SystemManager_OperateLog : System.Web.UI.Page
{
    List<OperateLog> list = new OperLogManager().GetAllOperateLog();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.GridView1.DataSource = list;
            this.GridView1.DataBind();
        }
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView theGrid = sender as GridView;   // refer to the GridView
        int newPageIndex = 0;
        newPageIndex = e.NewPageIndex;
        newPageIndex = newPageIndex < 0 ? 0 : newPageIndex;
        newPageIndex = newPageIndex >= theGrid.PageCount ? theGrid.PageCount - 1 : newPageIndex;

        theGrid.PageIndex = newPageIndex;
        this.GridView1.DataSource = list;
        this.GridView1.DataBind();
    }
    protected void cbAll_CheckedChanged(object sender, EventArgs e)
    {

        if (this.cbAll.Checked == true)
        {
            foreach (GridViewRow item in GridView1.Rows)
            {
                if (item.RowType == DataControlRowType.DataRow)
                {
                    CheckBox cb = (CheckBox)item.Cells[0].FindControl("cbgrid");
                    cb.Checked = true;

                }
            }
        }
        else
        {
            foreach (GridViewRow item in GridView1.Rows)
            {
                if (item.RowType == DataControlRowType.DataRow)
                {
                    CheckBox cb = (CheckBox)item.Cells[0].FindControl("cbgrid");
                    cb.Checked = false;

                }
            }
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow item in GridView1.Rows)
        {
            if (item.RowType == DataControlRowType.DataRow)
            {
                CheckBox cb = (CheckBox)item.Cells[0].FindControl("cbgrid");

                if (cb.Checked == true)
                {
                    new OperLogManager().DeleteOperateLog(Convert.ToInt32(item.Cells[1].Text));
                }
            }
        }
    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        DateTime begin = Convert.ToDateTime(this.txtBegin.Text.Trim());
        DateTime end = Convert.ToDateTime(this.txtEnd.Text.Trim());
        this.GridView1.DataSource = new OperLogManager().GetAllOperateLog(begin, end);
        this.GridView1.DataBind();
    }
    protected void rbDay_CheckedChanged(object sender, EventArgs e)
    {
        this.txtEnd.Text = this.txtBegin.Text;
    }
    protected void rbWeek_CheckedChanged(object sender, EventArgs e)
    {
        this.txtEnd.Text = DateTime.Now.ToShortDateString();
        int day = DateTime.Now.Day;
        string date;
        if (day - 6 > 0)
        {
            date = DateTime.Now.Year.ToString() + "-" + DateTime.Now.Month.ToString() + "-" + (day - 6).ToString();
            this.txtBegin.Text = date;
        }
        else
        {
            date = DateTime.Now.Year.ToString() + "-" + (DateTime.Now.Month - 1).ToString() + "-" + (30 - 6 + day).ToString();
            this.txtBegin.Text = date;
        }
    }
}